package com.yuncheng.spcyApi.spcyService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.selectXzxkApi.api.XzspJkApi;
import com.yuncheng.spcyApi.service.ISpcySqsxWjjlService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itextpdf.text.DocumentException;
import com.yuncheng.spcyApi.config.SpcyMinioUtils;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.XzXkMapper;
import com.yuncheng.spcyApi.schedule.ActiveScheduleTask;
import com.yuncheng.spcyApi.selectXzxkApi.constant.XzXkConstant;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspSqsxVo;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspSqsxXdrxxVo;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcySqsxQywjclService;
import com.yuncheng.spcyApi.service.ISpcySqsxQywjmlService;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspJkApiSbclxxlVo;
import com.yuncheng.spcyApi.vo.common.Result;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import utils.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ExecutionException;

@Component(value = "sqsxQywjclService")
public class SqsxQywjclService {

    private static final Logger log = LoggerFactory.getLogger(SqsxQywjclService.class);

    @Resource
    private ISpcySqsxQywjmlService spcySqsxQywjmlService;

    @Resource
    private ISpcySqsxQywjclService spcySqsxQywjclService;

    @Resource
    private XzXkMapper xzXkMapper;

    @Resource
    private ActiveScheduleTask activeScheduleTask;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private BusinessConstant businessConstant;

    @Resource
    private SpcyMinioUtils spcyMinioUtils;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    @Lazy
    private SpcyService spcyService;

    @Resource
    @Lazy
    private SpcyQyInfoService spcyQyInfoService;

    @Resource
    @Lazy
    private XzspJkApi xzspJkApi;

    @Resource
    private ISpcySqsxWjjlService spcySqsxWjjlService;

    /**
     * 测试同步
     */
    public void syncZwxtSqsxFlowByTest(String sqsxid,String fywflcode){

        XzspSqsxVo sqsxMap = xzXkMapper.selectSqsx(sqsxid);

        SpcySqsxBasic sqsx = new SpcySqsxBasic();
        sqsx.setId(sqsxMap.getId());
        sqsx.setFsxsqjbxxid(sqsxMap.getId());
        if (sqsxMap.getSqdbh() != null) {
            sqsx.setFsqdbh(sqsxMap.getSqdbh());
        }
        sqsx.setFywflcode(fywflcode);
        if (sqsxMap.getSxmc() != null) {
            sqsx.setFsxmc(sqsxMap.getSxmc());
        }

        if (sqsxMap.getSqsj() != null) {
            sqsx.setFsqsj(sqsxMap.getSqsj());
        }

        sqsx.setFywflname(SpcyConstant.getYwclNameByCode(fywflcode));
        sqsx.setFsyts("0");
        sqsx.setFxzspzt(SpcyConstant.SQSX_BASIC_XZSPZT_YES);

        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtil.isEmpty(qyList)){
            throw new BusinessException("申请企业为空！");
        }
        String xdrmc = qyList.get(0).getXdrmc();

        SpcyQyJbxx qy = spcyQyInfoService.syncZwfwXdrxx(xdrmc, 2);
        if (qy == null){
            throw new BusinessException("企业为空！");
        }
        sqsx.setFqyid(qy.getId());
        sqsx.setFqymc(qy.getFqymc());
        sqsx.setFtyshxydm(qy.getFtyshxydm());
        sqsx.setFqylxr(qy.getFqylxr());
        sqsx.setFqylxrdh(qy.getFqylxrdh());
        sqsx.setFxxdz(qy.getFxxdz());
        sqsx.setFqyzszcdz(qy.getFxxdz());

        spcyService.syncSqsxFlow(sqsx);

        // 同步申请材料
        this.syncZwxtSqclToLocalWjcl(sqsx.getId(),sqsx.getId());
    }

    /**
     * 测试同步
     */
    public void syncZwxtSqsxFlowByTest2(String sqdbh, String fywflcode){

        XzspSqsxVo sqsxMap = xzXkMapper.selectSqsxBySqdbh(sqdbh);

        SpcySqsxBasic sqsx = new SpcySqsxBasic();
        sqsx.setId(sqsxMap.getId());
        sqsx.setFsxsqjbxxid(sqsxMap.getId());
        if (sqsxMap.getSqdbh() != null) {
            sqsx.setFsqdbh(sqsxMap.getSqdbh());
        }
        sqsx.setFywflcode(fywflcode);
        if (sqsxMap.getSxmc() != null) {
            sqsx.setFsxmc(sqsxMap.getSxmc());
        }

        if (sqsxMap.getSqsj() != null) {
            sqsx.setFsqsj(sqsxMap.getSqsj());
        }

        sqsx.setFywflname(SpcyConstant.getYwclNameByCode(fywflcode));
        sqsx.setFsyts("0");
        sqsx.setFxzspzt(SpcyConstant.SQSX_BASIC_XZSPZT_YES);

        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxMap.getId());
        if (CollectionUtil.isEmpty(qyList)){
            throw new BusinessException("申请企业为空！");
        }
        String xdrmc = qyList.get(0).getXdrmc();

        SpcyQyJbxx qy = spcyQyInfoService.syncZwfwXdrxx(xdrmc, 2);
        if (qy == null){
            throw new BusinessException("企业为空！");
        }
        sqsx.setFqyid(qy.getId());
        sqsx.setFqymc(qy.getFqymc());
        sqsx.setFtyshxydm(qy.getFtyshxydm());
        sqsx.setFqylxr(qy.getFqylxr());
        sqsx.setFqylxrdh(qy.getFqylxrdh());
        sqsx.setFxxdz(qy.getFxxdz());
        sqsx.setFqyzszcdz(qy.getFxxdz());
        sqsx.setFfssj(new Date());
        sqsx.setFslsj(new Date());

        spcyService.syncSqsxFlow(sqsx);

        // 同步申请材料
        this.syncZwxtSqclToLocalWjcl(sqsx.getId(),sqsx.getId());
    }

    /**
     * 验证文件目录是否存在
     * @param mlmc 目录名称
     * @param sqsxid 申请事项id
     * @return
     * {
     *     flag: true 存在/flase,
     *     ml: 目录
     * }
     */
    private Map verifyClmlIsExists(String mlmc,String sqsxid){
        Map resultMap = new HashMap<>();

        LambdaQueryWrapper<SpcySqsxQywjml> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpcySqsxQywjml::getFmlmc, mlmc);
        wrapper.eq(SpcySqsxQywjml::getFsqsxid, sqsxid);
        wrapper.orderByAsc(SpcySqsxQywjml::getCreateTime);
        List<SpcySqsxQywjml> mlList = spcySqsxQywjmlService.list(wrapper);

        if (CollectionUtil.isNotEmpty(mlList)){
            resultMap.put("flag", true);
            resultMap.put("ml", mlList.get(0));
        }else {
            resultMap.put("flag", false);
            resultMap.put("ml", null);
        }

        return resultMap;
    }

    /**
     * 验证文件材料是否存在
     * @param wjid 文件id
     * @return true: 存在/false: 不存在
     */
    private boolean verifyWjclIsExists(String wjid){
        if (StringUtils.isBlank(wjid)){
            return true;
        }

        SpcySqsxQywjcl cl = spcySqsxQywjclService.getById(wjid);
        if (cl != null){
            return true;
        }

        return false;
    }

    /**
     * 新增企业文件目录
     * @param mlmc
     * @param sqsxid
     * @param sxsqjbxxid
     * @param pxh 排序
     * @return
     */
    public SpcySqsxQywjml addNewMl(String mlmc,String sqsxid,String sxsqjbxxid,String pxh){
        SpcySqsxQywjml qywjml = new SpcySqsxQywjml();
        qywjml.setId(GetUuIdUtils.ReplaceUuId());
        qywjml.setFmlmc(mlmc);
        qywjml.setFsqsxid(sqsxid);
        qywjml.setFsxsqjbxxid(sxsqjbxxid);
        qywjml.setFywbcscyj(SpcyCommonConstant.QY_WJML_YWYJ_NO);
        qywjml.setFywscyj(SpcyCommonConstant.QY_WJML_YWYJ_NO);
        if (StringUtils.isNotBlank(pxh)) {
            qywjml.setForder(Integer.valueOf(pxh));
        }
        spcySqsxQywjmlService.save(qywjml);

        return qywjml;
    }

    /**
     * 查询文件目录的文件材料-根据申请事项id
     * @param sqsxid 申请事项id
     * @param isReadable true: 目录可读
     * @param // isBqbz true: 需补齐补正目录
     * [{
     *     ml: 目录对象
     *     wjList: 文件记录
     * }]
     */
    public List<Map> queryQyWjclml(String sqsxid, boolean isReadable){
        return this.queryQyWjclml(sqsxid, isReadable, false);
    }
    public List<Map> queryQyWjclml(String sqsxid, boolean isReadable, boolean isBqbz){
        LambdaQueryWrapper<SpcySqsxQywjml> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpcySqsxQywjml::getFsqsxid, sqsxid);
        if (isBqbz){
            wrapper.isNotNull(SpcySqsxQywjml::getFbcscyj);
        }
        wrapper.orderByAsc(SpcySqsxQywjml::getForder);
        wrapper.orderByAsc(SpcySqsxQywjml::getCreateTime);
        List<SpcySqsxQywjml> mlList = spcySqsxQywjmlService.list(wrapper);

        List<Map> resultMlList = new ArrayList<>();
        for (int i=0; i<mlList.size(); i++){
            Map parMap = new HashMap();

            parMap.put("ml", mlList.get(i));

            LambdaQueryWrapper<SpcySqsxQywjcl> queryWrapper = null;
            if (isReadable){
                queryWrapper = new LambdaQueryWrapper<SpcySqsxQywjcl>()
                        .eq(SpcySqsxQywjcl::getFqyclmlid, mlList.get(i).getId())
                        .eq(SpcySqsxQywjcl::getFjckckzt, SpcyCommonConstant.QY_WJML_KCKZT_KD)
                        .orderByAsc(SpcySqsxQywjcl::getCreateTime);
            }else {
                if (isBqbz){
                    queryWrapper = new LambdaQueryWrapper<SpcySqsxQywjcl>()
                            .eq(SpcySqsxQywjcl::getFqyclmlid, mlList.get(i).getId())
                            .eq(SpcySqsxQywjcl::getFsfzxsc, SpcyCommonConstant.QY_WJCL_SFZXSC_NO)
                            .orderByAsc(SpcySqsxQywjcl::getCreateTime);
                }else {
                    queryWrapper = new LambdaQueryWrapper<SpcySqsxQywjcl>()
                            .eq(SpcySqsxQywjcl::getFqyclmlid, mlList.get(i).getId())
                            .orderByAsc(SpcySqsxQywjcl::getCreateTime);
                }
            }
            List<SpcySqsxQywjcl> clList = spcySqsxQywjclService.list(queryWrapper);
            parMap.put("wjList", clList);

            resultMlList.add(parMap);
        }

        /** 没有目录的情况下 */
        /*if (CollectionUtil.isEmpty(mlList) && !isBqbz){
            Map parMap = new HashMap();

            parMap.put("ml", "申报材料");

            LambdaQueryWrapper<SpcySqsxQywjcl> queryWrapper = null;
            if (isReadable){
                queryWrapper = new LambdaQueryWrapper<SpcySqsxQywjcl>()
                        .eq(SpcySqsxQywjcl::getFsqsxid, sqsxid)
                        .eq(SpcySqsxQywjcl::getFjckckzt, SpcyCommonConstant.QY_WJML_KCKZT_KD)
                        .orderByAsc(SpcySqsxQywjcl::getCreateTime);
            }else {
                if (isBqbz){
                    queryWrapper = new LambdaQueryWrapper<SpcySqsxQywjcl>()
                            .eq(SpcySqsxQywjcl::getFsqsxid, sqsxid)
                            .eq(SpcySqsxQywjcl::getFsfzxsc, SpcyCommonConstant.QY_WJCL_SFZXSC_NO)
                            .orderByAsc(SpcySqsxQywjcl::getCreateTime);
                }else {
                    queryWrapper = new LambdaQueryWrapper<SpcySqsxQywjcl>()
                            .eq(SpcySqsxQywjcl::getFsqsxid, sqsxid)
                            .orderByAsc(SpcySqsxQywjcl::getCreateTime);
                }
            }
            List<SpcySqsxQywjcl> clList = spcySqsxQywjclService.list(queryWrapper);
            parMap.put("wjList", clList);

            resultMlList.add(parMap);
        }*/

        return resultMlList;
    }

    /**
     * 默认新增目录和材料绑定默认id
     */
    private void addDefaultQywjmlAndBdCl(String sqsxid){

        List<SpcySqsxQywjml> mlList = spcySqsxQywjmlService.list(new LambdaQueryWrapper<SpcySqsxQywjml>()
                .eq(SpcySqsxQywjml::getFsqsxid, sqsxid));
        SpcySqsxQywjml qywjml = null;
        if (CollectionUtil.isEmpty(mlList)) {
            qywjml = this.addNewMl("申报材料", sqsxid, sqsxid, "1");

            LambdaUpdateWrapper<SpcySqsxQywjcl> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcySqsxQywjcl::getFsqsxid, sqsxid);
            updateWrapper.isNull(SpcySqsxQywjcl::getFqyclmlid);

            SpcySqsxQywjcl updateBean = new SpcySqsxQywjcl();
            updateBean.setFqyclmlid(qywjml.getId());
            spcySqsxQywjclService.update(updateBean, updateWrapper);
        }

    }

    /**
     * 提取政务服务系统-企业申请材料及目录
     * @param sbclxxLList 政务系统推送文件材料数据列表
     * @param sqsxid 申请事项id
     * @param sxsqjbxxid 事项申请基本信息id
     */
    @Async(value = "asyncNormalTask")
    public void syncZwxtQysqclAndMlApi(List<XzspJkApiSbclxxlVo> sbclxxLList, String sqsxid, String sxsqjbxxid){
        this.syncZwxtQysqclAndMl(sbclxxLList, sqsxid, sxsqjbxxid);
    }

    public void syncZwxtQysqclAndMl(List<XzspJkApiSbclxxlVo> sbclxxLList, String sqsxid, String sxsqjbxxid){
        // 同步申请材料
//        this.syncZwxtSqclToLocalWjcl(sqsxid, sxsqjbxxid);

        if (CollectionUtils.isEmpty(sbclxxLList)) {
            return;
        }

        List<SpcySqsxQywjcl> qywjclList = new ArrayList<>();

        for (XzspJkApiSbclxxlVo sbclxxL : sbclxxLList) {
            SpcySqsxQywjcl qywjcl = this.initWjmlAndWjcl(sqsxid, sxsqjbxxid, true, sbclxxL.getClmc(), sbclxxL.getId(), sbclxxL.getFjmc(), SpcyCommonConstant.QY_WJCL_CLLB_QYTJCL);
            if (qywjcl == null){
                continue;
            }
            qywjclList.add(qywjcl);
        }

        if (CollectionUtil.isNotEmpty(qywjclList)) {
            spcySqsxQywjclService.saveBatch(qywjclList);

            activeScheduleTask.tqZwxtFileToLocalFile(qywjclList);
        }
    }

    /**
     * 同步政务系统-申请材料-下载文件到本地
     * @param sqsxid 申请事项id
     * @param sxsqjbxxid 事项申请基本信息id
     * */
    public void syncZwxtSqclToLocalWjcl(String sqsxid,String sxsqjbxxid){
        List<SpcySqsxQywjcl> qywjclList = new ArrayList<>();

        List<Map<String, Object>> fjList = xzXkMapper.selectFjxxBybqbz(sqsxid, XzXkConstant.FJXX_FJLX_QYTJCL,null);

        if (CollectionUtil.isEmpty(fjList)){
            log.error("无申请材料，事项申请基本信息id： {}",sxsqjbxxid);
            return;
        }

        for (int i=0; i<fjList.size(); i++){

            String id = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_ID) != null){
                id = fjList.get(i).get(XzXkConstant.ZD_FJXX_ID).toString();
            }

            String clmc = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_CLMC) != null){
                clmc = fjList.get(i).get(XzXkConstant.ZD_FJXX_CLMC).toString();
            }

            String fjmc = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_FJYMC) != null){
                fjmc = fjList.get(i).get(XzXkConstant.ZD_FJXX_FJYMC).toString();
            }

            String fjbz = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_FJBZ) != null){
                fjbz = fjList.get(i).get(XzXkConstant.ZD_FJXX_FJBZ).toString();
            }else {
//                fjbz = SpcyCommonConstant.QY_WJCL_BZ_ZGCL;
            }

            String pxh = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_PXH) != null){
                pxh = fjList.get(i).get(XzXkConstant.ZD_FJXX_PXH).toString();
            }

            SpcySqsxQywjcl qywjcl = this.initWjmlAndWjcl(sqsxid, sxsqjbxxid, true, clmc, id, fjmc, SpcyCommonConstant.QY_WJCL_CLLB_QYTJCL, fjbz, pxh);
            if (qywjcl == null){
                continue;
            }
            qywjclList.add(qywjcl);
        }

        if (CollectionUtil.isNotEmpty(qywjclList)) {
            spcySqsxQywjclService.saveBatch(qywjclList);

            activeScheduleTask.tqZwxtFileToLocalFile(qywjclList);
        }
    }

    /**
     * 同步政务系统-补齐补正材料-下载文件到本地
     * @param sqsxid 申请事项id
     * @param sxsqjbxxid 事项申请基本信息id
     * */
    @Async(value = "asyncSimpleTask")
    public void syncZwxtBqbzclToLocalWjcl(String sqsxid,String sxsqjbxxid){

        List<SpcySqsxQywjcl> qywjclList = new ArrayList<>();

        List<Map<String, Object>> fjList = xzXkMapper.selectFjxxBybqbz(sxsqjbxxid, XzXkConstant.FJXX_FJLX_QYTJCL,XzXkConstant.FJXX_FJBZ_BQBZ);

        if (CollectionUtil.isEmpty(fjList)){
            log.error("无补齐补正材料，事项申请基本信息id： {}",sxsqjbxxid);
            return;
        }

        for (int i=0; i<fjList.size(); i++){

            String id = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_ID) != null){
                id = fjList.get(i).get(XzXkConstant.ZD_FJXX_ID).toString();
            }

            String clmc = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_CLMC) != null){
                clmc = fjList.get(i).get(XzXkConstant.ZD_FJXX_CLMC).toString();
            }

            String fjmc = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_FJYMC) != null){
                fjmc = fjList.get(i).get(XzXkConstant.ZD_FJXX_FJYMC).toString();
            }

            String fjbz = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_FJBZ) != null){
                fjbz = fjList.get(i).get(XzXkConstant.ZD_FJXX_FJBZ).toString();
            }else {
                fjbz = SpcyCommonConstant.QY_WJCL_BZ_BQBZCL;
            }

            String pxh = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_PXH) != null){
                pxh = fjList.get(i).get(XzXkConstant.ZD_FJXX_PXH).toString();
            }

            SpcySqsxQywjcl qywjcl = this.initWjmlAndWjcl(sqsxid, sxsqjbxxid, false, clmc, id, fjmc, SpcyCommonConstant.QY_WJCL_CLLB_BQBZCL, fjbz, pxh);
            if (qywjcl == null){
                continue;
            }
            qywjclList.add(qywjcl);
        }

        if (CollectionUtil.isNotEmpty(qywjclList)) {
            spcySqsxQywjclService.saveBatch(qywjclList);

            activeScheduleTask.tqZwxtFileToLocalFile(qywjclList);
        }
    }

    /**
     * 同步政务系统-整改材料-下载文件到本地
     * @param sqsxid 申请事项id
     * @param sxsqjbxxid 事项申请基本信息id
     * */
    @Async(value = "asyncSimpleTask")
    public void syncZwxtZgclToLocalWjcl(String sqsxid,String sxsqjbxxid){
//        List<SpcySqsxQywjcl> qywjclList = new ArrayList<>();
        List<SpcySqsxWjjl> qywjclList = new ArrayList<>();

        List<Map<String, Object>> fjList = xzXkMapper.selectFjxxBybqbz(sxsqjbxxid, XzXkConstant.FJXX_FJLX_QYZGCL,null);
//        List<Map<String, Object>> fjList = xzXkMapper.selectFjxxBybqbz(sqsxid, XzXkConstant.FJXX_FJLX_QYZGCL,XzXkConstant.FJXX_FJBZ_ZGCL);

        if (CollectionUtil.isEmpty(fjList)){
            log.error("无整改材料，事项申请基本信息id： {}",sxsqjbxxid);
            return;
        }

        for (int i=0; i<fjList.size(); i++){

            String id = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_ID) != null){
                id = fjList.get(i).get(XzXkConstant.ZD_FJXX_ID).toString();
            }

            /*String clmc = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_CLMC) != null){
                clmc = fjList.get(i).get(XzXkConstant.ZD_FJXX_CLMC).toString();
            }*/

            String fjmc = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_FJYMC) != null){
                fjmc = fjList.get(i).get(XzXkConstant.ZD_FJXX_FJYMC).toString();
            }

            /*String fjbz = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_FJBZ) != null){
                fjbz = fjList.get(i).get(XzXkConstant.ZD_FJXX_FJBZ).toString();
            }else {
                fjbz = SpcyCommonConstant.QY_WJCL_BZ_ZGCL;
            }

            String pxh = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_PXH) != null){
                pxh = fjList.get(i).get(XzXkConstant.ZD_FJXX_PXH).toString();
            }*/

            /*SpcySqsxQywjcl qywjcl = this.initWjmlAndWjcl(sqsxid, sxsqjbxxid, false, clmc, id, fjmc, SpcyCommonConstant.QY_WJCL_CLLB_BGCL, fjbz, pxh);
            if (qywjcl == null){
                continue;
            }*/

            String wjHz = Util.getWjHz(fjmc);
            String wjxsmc = Util.getWjmcNoHz(fjmc);
            String wjccmc = GetUuIdUtils.NotReplaceUuId() + wjHz;
            String cflj = "/" + DateConversionUtils.DateToMonthTwo(new Date()) + "/" + wjccmc;

            File file = this.downZwxtLocalWjcl(id, wjHz);
            if (file == null){
                try {
                    SpcySqsxQywjcl qywjcl = new SpcySqsxQywjcl();
                    qywjcl.setId(id);
                    qywjcl.setFcllj(localFileUploadUtils.getSqsxWjjlRelativeUrl(cflj));
                    xzspJkApi.tqSbcl(id, qywjcl, wjHz);
                }catch (Exception e){
                    log.error("提取政务服务-上报材料-错误：", e.getMessage());
                }

                continue;
            }
            String absUrl = file.getAbsolutePath();

            boolean flag = spcyMinioUtils.putFlagFile(localFileUploadUtils.getSqsxWjjlRelativeUrl(cflj), new File(absUrl), Util.getWjHz(cflj));
            if (flag) {
                spcySqsxWjjlService.initSaveSqsxWjjl(
                        sqsxid,
                        WjmlConstant.zgcl,
                        wjHz,
                        WjmlConstant.zgcl + "-" + wjxsmc,
                        wjccmc,
                        cflj,
                        SpcyConstant.QY_SQSX_WJJL_UPLOAD_FLAG);

            }
        }


        /*if (CollectionUtil.isNotEmpty(qywjclList)) {
            spcySqsxQywjclService.saveBatch(qywjclList);
            activeScheduleTask.tqZwxtFileToLocalFile(qywjclList);
        }*/

    }

    /**
     * 同步政务系统-撤件申请材料-下载文件到本地
     * @param sqsxid 申请事项id
     * @param sxsqjbxxid 事项申请基本信息id
     */
    @Async(value = "asyncSimpleTask")
    public void syncZwxtCjsqclToLocalWjcl(String sqsxid,String sxsqjbxxid){
//        List<SpcySqsxQywjcl> qywjclList = new ArrayList<>();

        List<Map<String, Object>> fjList = xzXkMapper.selectFjxxByCjsq(sxsqjbxxid,XzXkConstant.FJXX_CLMLID_CXSQ);

        if (CollectionUtil.isEmpty(fjList)){
            log.error("无撤件申请材料，事项申请基本信息id： {}",sxsqjbxxid);
            return;
        }

        for (int i=0; i<fjList.size(); i++){

            String id = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_ID) != null){
                id = fjList.get(i).get(XzXkConstant.ZD_FJXX_ID).toString();
            }

            // 撤件申请材料id
//            String clmc = SpcyCommonConstant.QY_WJCL_CLLB_QYCJCL;

            String fjmc = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_FJYMC) != null){
                fjmc = fjList.get(i).get(XzXkConstant.ZD_FJXX_FJYMC).toString();
            }

            /*String fjbz = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_FJBZ) != null){
                fjbz = fjList.get(i).get(XzXkConstant.ZD_FJXX_FJBZ).toString();
            }else {
                fjbz = SpcyCommonConstant.QY_WJCL_BZ_CJCL;
            }*/

            /*String pxh = "";
            if (fjList.get(i).get(XzXkConstant.ZD_FJXX_PXH) != null){
                pxh = fjList.get(i).get(XzXkConstant.ZD_FJXX_PXH).toString();
            }*/

            /*SpcySqsxQywjcl qywjcl = this.initWjmlAndWjcl(sqsxid, sxsqjbxxid, false, clmc, id, fjmc, SpcyCommonConstant.QY_WJCL_CLLB_QYCJCL, fjbz, pxh);
            if (qywjcl == null){
                continue;
            }
            qywjclList.add(qywjcl);*/

            String wjHz = Util.getWjHz(fjmc);
            String wjxsmc = Util.getWjmcNoHz(fjmc);
            String wjccmc = GetUuIdUtils.NotReplaceUuId() + wjHz;
            String cflj = "/" + DateConversionUtils.DateToMonthTwo(new Date()) + "/" + wjccmc;

            File file = this.downZwxtLocalWjcl(id, wjHz);
            if (file == null){
                try {
                    SpcySqsxQywjcl qywjcl = new SpcySqsxQywjcl();
                    qywjcl.setId(id);
                    qywjcl.setFcllj(localFileUploadUtils.getSqsxWjjlRelativeUrl(cflj));
                    xzspJkApi.tqSbcl(id, qywjcl, wjHz);
                }catch (Exception e){
                    log.error("提取政务服务-企业撤件上报材料-错误：", e.getMessage());
                }

                continue;
            }
            String absUrl = file.getAbsolutePath();

            boolean flag = spcyMinioUtils.putFlagFile(localFileUploadUtils.getSqsxWjjlRelativeUrl(cflj), new File(absUrl), Util.getWjHz(cflj));
            if (flag) {
                spcySqsxWjjlService.initSaveSqsxWjjl(
                        sqsxid,
                        WjmlConstant.qycjsqcl,
                        wjHz,
                        WjmlConstant.qycjsqcl + "-" + wjxsmc,
                        wjccmc,
                        cflj,
                        SpcyConstant.QY_SQSX_WJJL_UPLOAD_FLAG);

            }
        }

        /*if (CollectionUtil.isNotEmpty(qywjclList)) {
            spcySqsxQywjclService.saveBatch(qywjclList);

            activeScheduleTask.tqZwxtFileToLocalFile(qywjclList);
        }*/

    }

    /**
     * 初始化文件目录及材料
     * @param sqsxid 申请事项id
     * @param sxsqjbxxid 事项申请基本信息id
     * @param isJsq 是否接收前 （true接收前文件、false接收后文件）
     * @param clmc 材料目录名称
     * @param wjid 文件材料id
     * @param fjmc 材料附件名称
     * @param cllb 材料类别
     * @return
     */
    private SpcySqsxQywjcl initWjmlAndWjcl(String sqsxid,String sxsqjbxxid,boolean isJsq,String clmc,String wjid,String fjmc,String cllb){
        return this.initWjmlAndWjcl(sqsxid, sxsqjbxxid, isJsq, clmc, wjid, fjmc, cllb,null,"0");
    }

    /**
     *  初始化文件目录及材料
     *  @param cllb 材料类别
     *  @param bz 文件备注
     * @param pxh 目录排序
     */
    private SpcySqsxQywjcl initWjmlAndWjcl(String sqsxid,String sxsqjbxxid,boolean isJsq,String clmc,String wjid,String fjmc,String cllb,String bz, String pxh){

        if (this.verifyWjclIsExists(wjid)){
            return null;
        }

        // 目录名称
        String mlmc = Optional.ofNullable(clmc).orElseGet(() -> SpcyCommonConstant.QY_WJML_CLMC_DEFAULT);

        String mlid = "";
        // 验证文件目录是否存在
        Map mlMap = this.verifyClmlIsExists(mlmc, sqsxid);
        boolean isExistsMl = (boolean) mlMap.get("flag");
        if (isExistsMl){
            // 存在目录
            SpcySqsxQywjml ml = (SpcySqsxQywjml) mlMap.get("ml");
            mlid = ml.getId();
        }else {
            // 不存在目录
            SpcySqsxQywjml ml = this.addNewMl(mlmc, sqsxid, sxsqjbxxid, pxh);
            mlid = ml.getId();
        }

        SpcySqsxQywjcl qywjcl = new SpcySqsxQywjcl();
        qywjcl.setId(wjid);
        qywjcl.setFqyclmlid(mlid);
        qywjcl.setFsqsxid(sqsxid);
        qywjcl.setFclmc(fjmc);

        String cflj = "/"+SpcyCommonConstant.QY_WJCL_FILE_CFLJ_URL+"/"+ DateConversionUtils.DateToMonthTwo(new Date()) +"/"+qywjcl.getId()+ Util.getWjHz(qywjcl.getFclmc());

        qywjcl.setFcllj(cflj);

        // 事项接收前
        if (isJsq) {
            qywjcl.setFcljsqh(SpcyCommonConstant.QY_WJCL_CLQSQHZT_JSQ);
        }else {
            // 接收后
            qywjcl.setFcljsqh(SpcyCommonConstant.QY_WJCL_CLQSQHZT_JSH);
        }
        qywjcl.setFydzt(SpcyCommonConstant.QY_WJCL_YDZT_WD);

        qywjcl.setFcllb(cllb);

        qywjcl.setFbz(bz);

        return qywjcl;
    }

    /**
     * 提取政务系统文件到本地文件-对pdf文件添加水印
     * @param qywjclList 文件材料列表
     */
    public void tqZwxtFileToLocalFile(List<SpcySqsxQywjcl> qywjclList){
        if (CollectionUtil.isEmpty(qywjclList)){
            return;
        }

        for (SpcySqsxQywjcl qywjcl : qywjclList) {

            String wjHz = Util.getWjHz(qywjcl.getFclmc());
            File file = this.downZwxtLocalWjcl(qywjcl.getId(), wjHz);
            if (file == null){
                try {
                    xzspJkApi.tqSbcl(qywjcl.getId(), qywjcl, wjHz);
                }catch (Exception e){
                    log.error("提取政务服务-上报材料-错误：", e.getMessage());
                }

                continue;
            }

            /** pdf 加水印 */
            if (StringUtils.isNotBlank(wjHz) && wjHz.equals(".pdf")) {
                String destPdfUrl = localFileUploadUtils.getTempFileRootUrl() + "/";
                destPdfUrl += GetUuIdUtils.ReplaceUuId() + wjHz;

                FileOutputStream downloadFile = null;
                try {
                    downloadFile = new FileOutputStream(destPdfUrl);
                } catch (FileNotFoundException e) {
                    log.error("[FileNotFoundException]-{}",e.getMessage());
                }

                boolean flag = false;
                try {
                    PdfUtils.setWordMark(downloadFile, file.getAbsolutePath(), BusinessConstant.FILE_SY_NAME, true);
                    flag = true;
                } catch (DocumentException e) {
                    log.error("给pdf文件加水印出错-【DocumentException】：{}",e.getMessage());
                } catch (IOException e) {
                    log.error("给pdf文件加水印出错-【IOException】：{}",e.getMessage());
                }

                if (flag) {
                    spcyMinioUtils.putFile(qywjcl.getFcllj(), new File(destPdfUrl), ".pdf");
                }else {
                    spcyMinioUtils.putFile(qywjcl.getFcllj(), file, ".pdf");
                }

            }else {

                spcyMinioUtils.putFile(qywjcl.getFcllj(), file, wjHz);
            }
        }
    }

    /**
     * 将政务系统文件的文件-下载文件到本地
     * @param id 文件id
     * @param wjhz 文件后缀（.pdf/.doc/.docx）
     * */
    private File downZwxtLocalWjcl(String id, String wjhz){
        File file = null;

//        List<Map<String, Object>> fjList = xzXkMapper.selectFjxxById(id);
        List<String> fjList = xzXkMapper.selectFjxxCfljById(id);

        if (CollectionUtils.isNotEmpty(fjList)) {
//            String fjbclj = fjList.get(0).get(XzXkConstant.ZD_FJXX_FJBCLJ).toString();
            String fjbclj = fjList.get(0);

            if (StringUtils.isBlank(fjbclj)) {
                return file;
            }

            InputStream xzspFileObject = spcyMinioUtils.getXzspFileObject(fjbclj);
            if (xzspFileObject == null){
                return file;
            }
            file = localFileUploadUtils.asFile(xzspFileObject, wjhz);
        }

        return file;
    }

    /**
     * 下载企业申请材料
     * @param id 文件id
     */
    public void downloadQysqclFile(String id, boolean isAddSy, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(id)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }
        SpcySqsxQywjcl wjcl = spcySqsxQywjclService.getById(id);
        if (wjcl == null){
            log.error("下载企业申请材料-没有相匹配的材料数据: {}", id);
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的材料数据！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        if (StringUtils.isBlank(wjcl.getFcllj())){
            log.error("下载企业申请材料-附件不存在: {}", id);
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("附件不存在！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        /** 设置已读 */
        spcySqsxQywjclService.updateQysqclFileYdzt(id, wjcl.getFsqsxid(), wjcl.getFydzt(), wjcl.getFsfzxsc());

        if (isAddSy) {
            String symc = GetCurrentUserUtils.getCurrentUserName() + DateConversionUtils.DateToString(new Date());
            localFileUploadUtils.ejzZdyMinioFileUrlSy(wjcl.getFcllj(), "", symc, localFileUploadUtils.isEnableMinioWjjlFile(), response);

        }else {
            if (localFileUploadUtils.isEnableMinioWjjlFile()) {
                spcyMinioUtils.download(wjcl.getFcllj(), response);
            }else {
                localFileUploadUtils.ejzLocalFileByZdyFile(new File(localFileUploadUtils.getZdyFileAbsUrl(wjcl.getFcllj())), response);
            }
        }
    }

    /**
     * 汇总材料目录-审查意见
     * @param sqsxid 申请事项id
     * */
    public String summaryOpinionInScyj(String sqsxid){

        StringBuffer summaryOpinion = new StringBuffer();
        // 所有不为空的文件目录
        List<SpcySqsxQywjml> mlList = spcySqsxQywjmlService.list(new LambdaQueryWrapper<SpcySqsxQywjml>()
                .eq(SpcySqsxQywjml::getFsqsxid, sqsxid)
                .eq(SpcySqsxQywjml::getFywscyj, SpcyCommonConstant.QY_WJML_YWYJ_YES)
                .isNotNull(SpcySqsxQywjml::getFscyj)
                .orderByAsc(SpcySqsxQywjml::getForder)
                .orderByAsc(SpcySqsxQywjml::getCreateTime));

        for (int i = 0; i < mlList.size(); i++){
            if (i > 0) {
                summaryOpinion.append("\n");
            }
            // 四个空格 首行缩进
            summaryOpinion.append("    ");
            summaryOpinion.append(i + 1).append("、");

            // 新
            summaryOpinion.append("申报材料");
            summaryOpinion.append("“");
            summaryOpinion.append(mlList.get(i).getFmlmc());
            summaryOpinion.append("”");
            summaryOpinion.append(mlList.get(i).getFscyj());

            // 旧
            /*summaryOpinion.append(mlList.get(i).getFmlmc()).append("：");
            summaryOpinion.append(mlList.get(i).getFscyj());*/
        }

        if (StringPatternUtils.verifryIsEmptyStringBuffer(summaryOpinion)){
            return "";
        }
        return summaryOpinion.toString();
    }

    /**
     * 验证是否存在材料目录-审查意见
     * @return
     */
    public boolean verifyWjmlSfczScyj(String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            throw new BusinessException("参数为空！");
        }

        List<SpcySqsxQywjml> mlList = spcySqsxQywjmlService.list(new LambdaQueryWrapper<SpcySqsxQywjml>()
                .eq(SpcySqsxQywjml::getFsqsxid, sqsxid)
                .eq(SpcySqsxQywjml::getFywscyj, SpcyCommonConstant.QY_WJML_YWYJ_YES)
                .isNotNull(SpcySqsxQywjml::getFscyj));
        if (CollectionUtil.isNotEmpty(mlList)){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 更新文件目录内容
     * @param map (id: 目录id)
     */
    public void updateMl(HashMap<String,Object> map){
        String id = map.get("id").toString();
        SpcySqsxQywjml updateMl = spcySqsxQywjmlService.getById(id);

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateMl,map);
        } catch (IllegalAccessException e) {
            log.error("更新文件目录内容");
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        spcySqsxQywjmlService.updateById(updateMl);
    }

    /**
     * 同步文件目录审查意见-补充意见
     */
    public void syncWjmlScyjToBcscyj(String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return;
        }
        List<SpcySqsxQywjml> list = spcySqsxQywjmlService.list(new LambdaQueryWrapper<SpcySqsxQywjml>()
                .eq(SpcySqsxQywjml::getFsqsxid, sqsxid)
                .eq(SpcySqsxQywjml::getFywscyj, SpcyCommonConstant.QY_WJML_YWYJ_YES)
                .isNotNull(SpcySqsxQywjml::getFscyj));
        if (CollectionUtil.isNotEmpty(list)){
            /** 清空上次补充意见 */
            spcySqsxQywjmlService.updateEmptyBcyj(sqsxid);

            for (SpcySqsxQywjml ml : list){
                ml.setFbcscyj(ml.getFscyj());
                ml.setFywscyj("");
                ml.setFscyj("");
                spcySqsxQywjmlService.updateById(ml);
            }
        }

    }

    /**
     * 打包下载zip-企业提交材料
     * @param sqsxid 申请事项id
     */
    public void downloadZipInQytjcl(String sqsxid, HttpServletResponse response){
        LambdaQueryWrapper<SpcySqsxQywjml> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpcySqsxQywjml::getFsqsxid, sqsxid);
        wrapper.orderByAsc(SpcySqsxQywjml::getCreateTime);
        List<SpcySqsxQywjml> mlList = spcySqsxQywjmlService.list(wrapper);

        List<File> fileList = new ArrayList<>();
        List<String> fileNameList = new ArrayList();

        for (int j=0; j<mlList.size(); j++){
            List<SpcySqsxQywjcl> clList = spcySqsxQywjclService.list(new LambdaQueryWrapper<SpcySqsxQywjcl>()
                    .eq(SpcySqsxQywjcl::getFqyclmlid, mlList.get(j).getId())
                    .orderByAsc(SpcySqsxQywjcl::getCreateTime));
            for (int i=0; i<clList.size(); i++){
                if (StringUtils.isNotBlank(clList.get(i).getFcllj())) {
                    String wjHz = Util.getWjHz(clList.get(i).getFclmc());

                    File file = null;
                    if (localFileUploadUtils.isEnableMinioWjjlFile()){
                        InputStream object = spcyMinioUtils.getObject(clList.get(i).getFcllj());
                        if (object == null){
                            continue;
                        }
                        file = localFileUploadUtils.asFile(object, wjHz);
                    }else {
                        file = new File(localFileUploadUtils.getSysRootUrl() + clList.get(i).getFcllj());
                    }

                    fileList.add(file);
                    if (StringUtils.isNotBlank(clList.get(i).getFbz())) {
                        fileNameList.add(mlList.get(j).getFmlmc() + "/(" + clList.get(i).getFbz()+")"+clList.get(i).getFclmc());
                    }else {
                        fileNameList.add(mlList.get(j).getFmlmc() + "/" + clList.get(i).getFclmc());
                    }
                }
            }
        }

        // 输入zip文件路径
        StringBuffer outZipUrl = new StringBuffer();
        outZipUrl.append(businessConstant.TEMPORARY_UPLOAD_MKDIR_ROOT);
        outZipUrl.append("/");
        outZipUrl.append(SpcyConstant.TEMP_WJJL_CHILD_URL);
        outZipUrl.append("/");
        outZipUrl.append(GetUuIdUtils.ReplaceUuId());
        outZipUrl.append(".zip");
        try {
            ZipCompressUtils.compressFileList(outZipUrl.toString(),0,fileList,fileNameList);
        } catch (IOException e) {
            log.error("下载文件ZIP出错->[IOException]->{}",e.getMessage());
        } catch (ExecutionException e) {
            log.error("下载文件ZIP出错->[ExecutionException]->{}",e.getMessage());
        } catch (InterruptedException e) {
            log.error("下载文件ZIP出错->[InterruptedException]->{}",e.getMessage());
        }
        ZipCompressUtils.downloadZip(outZipUrl.toString(),response);
    }

    // 创建目录，返回目录id
    public String addZdyWjml(String sqsxid){
        List<SpcySqsxQywjml> list = spcySqsxQywjmlService.list(new LambdaQueryWrapper<SpcySqsxQywjml>()
                .eq(SpcySqsxQywjml::getFsqsxid, sqsxid));
        if (CollectionUtil.isEmpty(list)){

            return addNewMl("申请材料", sqsxid, sqsxid, "1").getId();
        }else {
            return list.get(0).getId();
        }
    }

    /**
     * 中心上传文件-企业文件材料
     * @param sqsxid 申请事项id
     * @param isOnlyPdf true: 不能上传文档类型文件
     */
    public void uploadFile(MultipartFile[] uploadFile, String sqsxid, boolean isOnlyPdf){
        if (uploadFile == null || uploadFile.length == 0) {
            throw new BusinessException("上传失败,请先选择文件！");
        }

        for (MultipartFile file : uploadFile) {

            SpcySqsxQywjcl qywjcl = new SpcySqsxQywjcl();

            String originalFilename = file.getOriginalFilename();

            // 文件类型
            String wjlx = originalFilename.substring(originalFilename.lastIndexOf("."));
            wjlx = wjlx.toLowerCase();

            if (isOnlyPdf && (wjlx.equalsIgnoreCase(".doc") || wjlx.equalsIgnoreCase(".docx"))){
                throw new BusinessException("请上传pdf文件/图片！");
            }

            // 文件名
//            String fileName = originalFilename.substring(0, originalFilename.lastIndexOf("."));

            qywjcl.setId(GetUuIdUtils.ReplaceUuId());
            qywjcl.setFcllb(SpcyCommonConstant.QY_WJCL_CLLB_QTCL); // 文件显示名称
            qywjcl.setFcljsqh(SpcyCommonConstant.QY_WJCL_CLQSQHZT_JSH);
            qywjcl.setFsfzxsc(SpcyCommonConstant.QY_WJCL_SFZXSC_YES);
            qywjcl.setFjckckzt(SpcyCommonConstant.QY_WJML_KCKZT_KD);
            qywjcl.setFydzt(SpcyCommonConstant.QY_WJCL_YDZT_WD);
            qywjcl.setFclmc(originalFilename);

            String wjccmc = qywjcl.getId() + wjlx;

            String wjml = SpcyCommonConstant.QY_WJCL_FILE_CFLJ_URL+"/"+DateConversionUtils.DateToMonthTwo(new Date());
            localFileUploadUtils.uploadZdyFile(file, wjccmc, wjml, localFileUploadUtils.isEnableMinioWjjlFile()); // 上传文件

            qywjcl.setFsqsxid(sqsxid);
            qywjcl.setFcllj(wjlx);
            String cflj = "/" + wjml + "/" + wjccmc;
            qywjcl.setFcllj(cflj);

            qywjcl.setFqyclmlid(addZdyWjml(sqsxid));
            spcySqsxQywjclService.save(qywjcl);


            String czContent = "上传文件-"+originalFilename+": ";
            Map<String, Object> wjjlMap = ObjectToMapUtils.objectToMap(qywjcl);
            czContent += LogUtils.MapToStringMethod(wjjlMap);
            spcyLogService.addLogs(LogConstant.LOGS_SQSX_BASIC, czContent, sqsxid, "中心上传文件-企业文件材料", SpcyConstant.LOGS_SYS_TYPE_BC);
        }

    }

    /**
     * 中心上传文件-企业文件材料
     * @param sqsxid 申请事项id
     * @param clmlid 材料目录id
     * @param isOnlyPdf true: 不能上传文档类型文件
     */
    public void uploadQywjclByQy(MultipartFile[] uploadFile, String sqsxid, String clmlid, boolean isOnlyPdf){
        if (uploadFile == null || uploadFile.length == 0) {
            throw new BusinessException("上传失败,请先选择文件！");
        }

        for (MultipartFile file : uploadFile) {

            SpcySqsxQywjcl qywjcl = new SpcySqsxQywjcl();

            String originalFilename = file.getOriginalFilename();

            // 文件类型
            String wjlx = originalFilename.substring(originalFilename.lastIndexOf("."));
            wjlx = wjlx.toLowerCase();

            if (isOnlyPdf && (wjlx.equals(".doc") || wjlx.equals(".docx"))){
                throw new BusinessException("请上传pdf文件/图片！");
            }

            // 文件名
//            String fileName = originalFilename.substring(0, originalFilename.lastIndexOf("."));

            qywjcl.setId(GetUuIdUtils.ReplaceUuId());
            qywjcl.setFcllb(SpcyCommonConstant.QY_WJCL_CLLB_QYTJCL); // 文件显示名称
            qywjcl.setFcljsqh(SpcyCommonConstant.QY_WJCL_CLQSQHZT_JSH);
            qywjcl.setFsfzxsc(SpcyCommonConstant.QY_WJCL_SFZXSC_NO);
            qywjcl.setFclmc(originalFilename);
            qywjcl.setFqyclmlid(clmlid); // 企业材料目录id
            qywjcl.setFydzt(SpcyCommonConstant.QY_WJCL_YDZT_WD);
            qywjcl.setFbz(SpcyCommonConstant.QY_WJCL_CLLB_BQBZCL);

            String wjccmc = qywjcl.getId() + wjlx;

            String wjml = SpcyCommonConstant.QY_WJCL_FILE_CFLJ_URL+"/"+DateConversionUtils.DateToMonthTwo(new Date());
            localFileUploadUtils.uploadZdyFile(file, wjccmc, wjml, localFileUploadUtils.isEnableMinioWjjlFile()); // 上传文件

            qywjcl.setFsqsxid(sqsxid);
            qywjcl.setFcllj(wjlx);
            String cflj = "/" + wjml + "/" + wjccmc;
            qywjcl.setFcllj(cflj);
            spcySqsxQywjclService.save(qywjcl);


            String czContent = "上传文件-"+originalFilename+": ";
            Map<String, Object> wjjlMap = ObjectToMapUtils.objectToMap(qywjcl);
            czContent += LogUtils.MapToStringMethod(wjjlMap);
            spcyLogService.addLogs(LogConstant.LOGS_SQSX_BASIC, czContent, sqsxid, "企业上传文件-企业文件材料", SpcyConstant.LOGS_SYS_TYPE_BC);
        }

    }

    /** 查询允许现场检查查看文件材料列表 */
    public List<SpcySqsxQywjcl> queryKckWjclList(String sqsxid){
        List<SpcySqsxQywjcl> list = spcySqsxQywjclService.list(new LambdaQueryWrapper<SpcySqsxQywjcl>()
                .eq(SpcySqsxQywjcl::getFsqsxid, sqsxid)
                .eq(SpcySqsxQywjcl::getFjckckzt, SpcyCommonConstant.QY_WJML_KCKZT_KD)
                .orderByDesc(SpcySqsxQywjcl::getCreateTime));

        return list;
    }

    /** 查询企业所有申请文件材料 */
    public List<SpcySqsxQywjcl> queryAdlQySqWjclList(String sqsxid){
        List<SpcySqsxQywjcl> list = spcySqsxQywjclService.list(new LambdaQueryWrapper<SpcySqsxQywjcl>()
                .eq(SpcySqsxQywjcl::getFsqsxid, sqsxid)
                .orderByDesc(SpcySqsxQywjcl::getCreateTime));

        return list;
    }

    /** 选择文件材料-现场检查文件可查看
     * @param ids 文件材料ids
     * */
    public Result selectWjclKck(String ids){
        if (StringUtils.isBlank(ids)){
            return Result.error("文件参数为空！");
        }

        List<String> idList = StringPatternUtils.getListByIds(ids);

        LambdaUpdateWrapper<SpcySqsxQywjcl> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(SpcySqsxQywjcl::getId, idList);

        SpcySqsxQywjcl update = new SpcySqsxQywjcl();
        update.setFjckckzt(SpcyCommonConstant.QY_WJML_KCKZT_KD);
        spcySqsxQywjclService.update(update, updateWrapper);

        return Result.ok();
    }

    /**
     * 合并事项-选择企业材料-现场检查可查看材料
     */
    public Result selectHbkckcl(String ids, String sqsxid){
        if (StringUtils.isBlank(ids)){
            return Result.error("文件参数为空！");
        }
        if (StringUtils.isBlank(sqsxid)){
            return Result.error("事项参数为空！");
        }

        List<SpcySqsxQywjcl> qywjclList = spcySqsxQywjclService.listByIds(StringPatternUtils.getListByIds(ids));
        if (CollectionUtil.isEmpty(qywjclList)){
            return Result.error("没有相匹配的材料！");
        }

        /** 复制材料记录-不复制材料文件 */
        List<SpcySqsxQywjcl> newClList = new ArrayList<>();
        for (SpcySqsxQywjcl oldBean : qywjclList){
            SpcySqsxQywjcl newBean = new SpcySqsxQywjcl();
            BeanUtil.copyProperties(oldBean, newBean);
            newBean.setId(GetUuIdUtils.NotReplaceUuId());
            newBean.setFsfzxsc(SpcyCommonConstant.QY_WJCL_SFZXSC_YES);
            newBean.setFqyclmlid("");
            newBean.setFsqsxid(sqsxid);
            newBean.setFjckckzt(SpcyCommonConstant.QY_WJML_KCKZT_KD);
            newBean.setFbz(SpcyCommonConstant.QY_WJCL_FBZ_HBSX);  // 后续删除材料会用到

            newClList.add(newBean);
        }
        spcySqsxQywjclService.saveBatch(newClList);

        return Result.ok();
    }

    /** 取消文件材料-现场检查可查看状态
     * @param id 文件材料id
     * */
    public Result cancelSelectWjclKck(String id){
        if (StringUtils.isBlank(id)){
            return Result.error("文件参数为空！");
        }

        LambdaUpdateWrapper<SpcySqsxQywjcl> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxQywjcl::getId, id);

        SpcySqsxQywjcl update = new SpcySqsxQywjcl();
        update.setFjckckzt(SpcyCommonConstant.QY_WJML_KCKZT_BKD);
        spcySqsxQywjclService.update(update, updateWrapper);

        return Result.ok();
    }

    /** 删除企业文件材料
     * @param id 文件材料id
     * @param isZxsc true: 中心
     * */
    public Result deleteQywjclById(String id, boolean isZxsc){
        if (StringUtils.isBlank(id)){
            return Result.error("文件参数为空！");
        }

        SpcySqsxQywjcl wjcl = spcySqsxQywjclService.getById(id);
        if (wjcl == null){
            return Result.error("文件记录不存在！");
        }

        /** 合并事项文件删除记录-不删除材料 */
        if (StringUtils.isNotBlank(wjcl.getFbz()) && wjcl.getFbz().equals(SpcyCommonConstant.QY_WJCL_FBZ_HBSX)){
            spcySqsxQywjclService.removeById(id);

            return Result.ok();
        }

        if (isZxsc) {
            if (StringUtils.isBlank(wjcl.getFsfzxsc()) || !wjcl.getFsfzxsc().equals(SpcyCommonConstant.QY_WJCL_SFZXSC_YES)) {
                return Result.error("无操作权限，只有中心上传的材料才能删除！");
            }
        }else {
            if (StringUtils.isBlank(wjcl.getFsfzxsc()) || !wjcl.getFsfzxsc().equals(SpcyCommonConstant.QY_WJCL_SFZXSC_NO)){
                return Result.error("无操作权限，只有申请之后上传的材料才能删除！");
            }
        }

        spcySqsxQywjclService.removeById(id);
        if (StringUtils.isNotBlank(wjcl.getFcllj())) {
            localFileUploadUtils.deleteLocalFileUrlByZdy(wjcl.getFcllj(), localFileUploadUtils.isEnableMinioWjjlFile());
        }

        String log = "删除企业文件材料：";
        log += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(wjcl));
        spcyLogService.addLogs(LogConstant.LOGS_SQSX_BASIC, log, wjcl.getFsqsxid(), "删除企业文件材料", SpcyConstant.LOGS_SYS_TYPE_SC);

        return Result.ok();
    }

    /**
     * 批量删除企业文件材料-任务登记
     * */
    public void batchDeleteRwdjQywjcl(String sqsxid){
        List<SpcySqsxQywjcl> list = spcySqsxQywjclService.list(new LambdaQueryWrapper<SpcySqsxQywjcl>()
                .eq(SpcySqsxQywjcl::getFsqsxid, sqsxid));
        if (CollectionUtil.isEmpty(list)){
            return;
        }

        String log = "";
        for (SpcySqsxQywjcl bean : list){
            if (StringUtils.isNotBlank(bean.getFcllj())) {
                localFileUploadUtils.deleteLocalFileUrlByZdy(bean.getFcllj(), true);
            }

            log = "";
            log = "批量删除企业文件材料-任务登记：";
            log += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
            spcyLogService.addLogs(LogConstant.LOGS_SQSX_BASIC, log, sqsxid, "批量删除企业文件材料-任务登记", SpcyConstant.LOGS_SYS_TYPE_SC);
        }

        spcySqsxQywjclService.remove(new LambdaQueryWrapper<SpcySqsxQywjcl>()
                .eq(SpcySqsxQywjcl::getFsqsxid, sqsxid));
    }

    /**
     * 批量设置有无审查意见
     * @param sqsxid 申请事项id
     * @param isYes true：有意见/false: 无意见
     */
    public void updateYwScyj(String sqsxid, boolean isYes){
        if (StringUtils.isBlank(sqsxid)){
            throw new BusinessException("事项参数为空！");
        }

        LambdaUpdateWrapper<SpcySqsxQywjml> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxQywjml::getFsqsxid, sqsxid);

        SpcySqsxQywjml updateMl = new SpcySqsxQywjml();
        if (isYes) {
            updateMl.setFywscyj(SpcyCommonConstant.QY_WJML_YWYJ_YES);
        }else {
            updateMl.setFywscyj(SpcyCommonConstant.QY_WJML_YWYJ_NO);
        }
        spcySqsxQywjmlService.update(updateMl, updateWrapper);
    }
}
